package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.PurchaseRecord;
import com.cn.transfer.common.entity.SalesRecord;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.StRDRecordMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/10/11 17:06
 * @Description: 销售出库新增
 */
@Component
public class SalesRecordAddXxlJob {

    @Autowired
    private StRDRecordMapper stRDRecordMapper;

    @XxlJob("SalesRecordAdd")
    public void salesRecordAdd() {
        List<SalesRecord> salesRecords = null;
        String param = XxlJobHelper.getJobParam();
        if (param.equals("v")){
            salesRecords  = stRDRecordMapper.salesRecordNotNullDayList();
        }else {
            salesRecords  = stRDRecordMapper.notList();
        }
        Header[] headers = ConnmeUtils.createHeaders();
        List<Map<String, Object>> datas = new ArrayList<>();
        for (SalesRecord salesRecord : salesRecords) {
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("app_id", "6523b413080f4ccc9ea1ce98");
            parameters.put("entry_id", "652612d5289b41f28f99d487");
            List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameters, headers, salesRecord.getCode(), "code");
            // 查询是否已经存在简道云
            if (jdyPurchaseOrders.size() > 0) {
                continue;
            }
            datas.add(ConnmeUtils.convertEntityToMap(salesRecord));
        }
        final int batchSize = 50;  // 每批处理的数据量
        int dataSize = datas.size();
        int numberOfBatches = dataSize % batchSize == 0 ? dataSize / batchSize : dataSize / batchSize + 1;

        for (int batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
            int startIndex = batchIndex * batchSize;
            int endIndex = Math.min(startIndex + batchSize, dataSize);

            List<Map<String, Object>> lista = datas.subList(startIndex, endIndex);

            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "6523b413080f4ccc9ea1ce98");
            parameter.put("entry_id", "652612d5289b41f28f99d487");
            parameter.put("data_list", lista);

            String jsonSale = JSONUtil.toJsonStr(parameter);

            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/batch_create", headers, jsonSale);
        }
    }
}
